from pymysql import *
import time
import datetime



class SoluTion(object):

    def __init__(self):
        # 创建connect对象
        self.conn = connect(
            host = 'localhost',
            port = 3306,
            database ='python_test_1',
            user = 'root',
            password = '123123',
            charset = 'utf8'
        )
        # 获得cursor对象，对数据库中的内容操作的把手
        self.cursor = self.conn.cursor()
        self.flag = 0

    def shut_conn(self):
        '''关闭光标，关闭连接'''
        self.cursor.close()
        self.conn.close()

    def menu(self):
        '''菜单显示'''
        print('-------------京东--------------\n')
        print('-------1.登陆---2.注册---------\n')
        print('------------0.exit-------------\n')
        return input('请输入：')

    def show_items(self, sql, judge_num):
        '''显示所有sql语句对应的信息'''
        # 利用例去操作数据库
        self.cursor.execute(sql)   # 返回影响行数
        content = self.cursor.fetchall()
        if judge_num:
            for line in content:
                print(line)
        elif judge_num == 0:
            return content

    def select_item(self, customer_id):
        '''顾客选取要买的商品'''
        while True:
        # 显示全部物品信息
            sql_show = 'select * from goods as g inner join (select * from goods_cates) as c inner join (select * from goods_brands) as b where g.cate_id=c.id and g.brand_id=b.id'
            solution.show_items(sql_show,1)
            op_item = int(input('请输入要购买商品的id(tab 0 for exit)：'))
            sql_g = 'select * from goods where id={}'.format(op_item)
            ret = solution.show_items(sql_g, 0)
            print(ret[0])
            op = int(input('请输入(tab 仍意键 for return or tab 1 for confirm or tab q for exit): '))
            if op == '1':
                counts = int(input('输入购买数量：'))
                print('-------paying')
                for i in range(5):
                    print('>>')
                    time.sleep(0.5)
                # 准备orders表
                sql_orders_time = 'select now()'  # 准备时间
                ret_time = solution.show_items(sql_orders_time, 0)
                # print(ret_time)     # ((datetime.datetime(2022, 5, 2, 14, 4, 59),),)
                # print(ret_time[0][0])
                sql_orders = "insert into orders values(0,'{}',{})".format(ret_time[0][0],customer_id)
                solution.add(sql_orders)
                # 准备order_detail表
                sql_order_id = "select id from orders where order_date_time='{}' and customer_id={}".format(ret_time[0][0],customer_id)
                ret_order_id = solution.show_items(sql_order_id, 0)
                sql_order_detail = 'insert into order_detail values(0,{},{},{})'.format(ret_order_id[0][0], op_item, counts)
                solution.add(sql_order_detail)
                print('订单生成成功》》》》》》》》》')
                solution.menu_show_order()
            elif op == 'q':
                break

    def menu_show_order(self):
        '''显示订单详情'''
        sql_show_good_info = 'select order_id,good_id from order_detail inner join orders  '

    def judge_cum(self,sql,i_count,i_password):
        '''判断是否存在账号验证账号'''
        ret = solution.show_items(sql, 0)
        flag = 0
        for count_c,password_c in ret:
            if count_c:
                if count_c == i_count and password_c == i_password:
                    flag = 2
                    break
                else:
                    flag = 1
        if flag == 0:
            print('没有该用户，请先注册>>>>>>\n')
            solution.register_count()
        elif flag == 2:
            print('---------->>>loaiding............\n')
        elif flag ==1:
            print('输入错误!!!!!!!!!!!!!!!!!!!!!\n')

    def register_count(self):
        '''用户注册'''
        print('-----------注册------------\n')
        print('-----------0.exit----------\n')
        i_count = int(input('请输入账号:'))
        i_password = input('请输入密码:')
        again_password = input('请再次输入密码:')
        if i_password == again_password:
            print('接下来编辑您的个人信息-------->>>>>>>>>\n')
            i_name = input('请输入您的姓名:')
            i_addr = input('请输入您的地址:')
            i_tel = input('请输入你的电话:')
            sql_add_account = "insert into customer values(0,'{}','{}','{}','{}','{}')".format(i_name, i_addr, i_tel, i_count, i_password)
            solution.add(sql_add_account)
            print('----------{}注册成功>>>>>>>>>>\n'.format(i_count))
            content_id = solution.load()
            try:
                solution.select_item(content_id[0][0])
            except Exception as ret:
                pass
        elif i_count == 0:
            return 0
        

    def load(self):
        '''用户登陆'''
        print('---------登陆-----------\n')
        # 请用户输入帐号和密码
        count = int(input('请输入账号：'))
        password = input('请输入密码：')
        try:
            # 查看用户表中有没有该用户的信息，确认登陆或去注册
            sql_judge = 'select count,password from customer'
            solution.judge_cum(sql_judge, count, password)
            sql_ctm_id = 'select id from customer where count={} and password={}'.format(count, password)
            return solution.show_items(sql_ctm_id, 0)
        except Exception as ret:
            print('出错，正在返回》》》》》》》》》')
    
    def add(self, sql):
        '''增加记录'''
        self.cursor.execute(sql)
        self.conn.commit()     # 连接上去提交内容

    def main(self):
        '''逻辑控制'''
        while True:
            # 制作菜单
            op = solution.menu()
            # 登陆/注册
            if op == '1':
                # 登陆
                customer_id = solution.load()
                try:
                    solution.select_item(customer_id[0][0])
                except Exception as ret:
                    pass    
            elif op == '2':
                # 注册
                ret = solution.register_count()
                if ret == 0:
                    break
            elif op == '0':
                # 退出 
                break
            else:
                print('输入错误!!!!!!!\n')
            # 查看订单信息
        # 关闭光标，关闭连接
        solution.shut_conn()
if __name__ == '__main__':
    solution = SoluTion()
    solution.main()
    

